package open.witype.filetiper.activity.home;

import android.os.Bundle;

import open.witype.filetiper.R;
import open.witype.filetiper.activity.BaseActivity;
import open.witype.filetiper.activity.home.adapter.HomeFragmentAdapter;
import open.witype.filetiper.fragment.menu.MenuFragment;
import open.witype.filetiper.model.FavoItemBeam;
import open.witype.filetiper.model.FileItemBean;
import open.witype.filetiper.inter.OnBackPressListener;
import open.witype.filetiper.activity.home.interf.OnItemClickListener;

/**
 * Created by Typer on 2015/5/10 0010.
 *
 */
public class HomeActivity extends BaseActivity {

    private OnBackPressListener listener;

    private HomeFragmentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();
        initSlidingMenu();
    }

    public void init(){
        adapter = new HomeFragmentAdapter(getSupportFragmentManager(),R.id.above_content,getApplicationContext());
    }

    public void initSlidingMenu(){
        getSupportFragmentManager().beginTransaction().add(R.id.menu_content,new MenuFragment()).commit();
    }

    public OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClickCallback(FileItemBean itemBean, int position) {
            if(itemBean.isFile()) {
                show("file name = " + itemBean.getFileName());
            }
            else {
                adapter.addItem(itemBean.getPath());
            }
        }

        @Override
        public void onItemClickCallback(FavoItemBeam itemBeam, int position) {

        }
    };

    public void setListener(OnBackPressListener listener) {
        this.listener = listener;
    }

    @Override
    public void onBackPressed() {
            if (listener!=null) {
            listener.OnBackPressed();
        }
        else{
            boolean isLastBack = adapter.deleteItem();
            if(isLastBack)
                super.onBackPressed();
        }
    }
}
